//
//  D_TimeViewController.m
//  project_delicious
//
//  Created by 章历杰 on 15/3/26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "D_TimeViewController.h"
#import "AFNetworking.h"
#import "UIColor+AddColor.h"
#import "D_TimeCell.h"
#import "OneOfDTViewController.h"
#import "MBProgressHUD.h"
@interface D_TimeViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) MBProgressHUD *HUD;
@end

@implementation D_TimeViewController

- (void)dealloc
{
    [_num release];
    [_name release];
    [_array release];
    [_HUD release];
    [super dealloc];
}
//解析数据
- (void)startAnalyze:(NSString *)str
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        self.array = [NSMutableArray array];
        self.array = [[responseObject objectForKey:@"data"] objectForKey:@"topic_item"];
        
        //页面铺一个tableView
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.backgroundColor = [UIColor clearColor];
        tableView.separatorColor = [UIColor clearColor];
        [self.view addSubview:tableView];
        [tableView release];
        
        //        //页面名字
        //        UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-60, 25, 120, 40)];
        //        name.text = self.name;
        //        name.layer.borderColor = [[UIColor colorFromHexCode:@"#33475f"] CGColor];
        //        name.textAlignment = NSTextAlignmentCenter;
        //        name.layer.borderWidth = 2;
        //        name.layer.cornerRadius = 20;
        //        [self.view addSubview:name];
        //        [name release];
        
        
        //返回按钮
        UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 40, 40)];
        back.image = [UIImage imageNamed:@"backToRecomm.png"];
        back.layer.masksToBounds = YES;
        back.layer.cornerRadius = 20;
        back.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackAction)];
        [back addGestureRecognizer:tap];
        [tap release];
        [self.view addSubview:back];
        [back release];
        
        [_HUD hide:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"错误");
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"IMG_1345.JPG"]];
    
    
    NSString *string1 = @"http://yinweiapi.paidui.com:81/jms/fetchData.do?procedureName=app_topic_topic_list&parameters=%7B%22_end%22%3A%2210%22%2C%22_start%22%3A%220%22%2C%22_type_id%22%3A%22";
    NSString *string2 = @"%22%7D";
 
    NSString *str = [NSString stringWithFormat:@"%@%@%@", string1,self.num,string2];
    
    [self startAnalyze:str];
    
    self.HUD = self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    _HUD.labelText = @"Loading";
    //_HUD.color = [UIColor clearColor];
    _HUD.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:_HUD];
    [_HUD release];
    [_HUD show:YES];
    
}
- (void)tapBackAction
{
    [self.navigationController popViewControllerAnimated:NO];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reuse";
    D_TimeCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[D_TimeCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
    }
    cell.mDic = [self.array objectAtIndex:indexPath.item];
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 250;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OneOfDTViewController *one = [[OneOfDTViewController alloc] init];
    one.idNum = [[self.array objectAtIndex:indexPath.item] objectForKey:@"topic_id"];
    one.mDic = [self.array objectAtIndex:indexPath.item];
    one.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:one animated:NO];
    [one release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
